Перейти к основному содержимому
Версия: 1.7.5

Тестирование

Запуск тестов SDK

cd ModuleSDK
pip install pytest
CONFIG_FILE=config/config_example.yaml pytest module_sdk/test/ -v

Тестовые фикстуры (conftest.py)

SDK использует специальную фикстуру storage_module, которая:

  1. Создает временный config.yaml

  2. Устанавливает CONFIG_FILE env

  3. Переимпортирует module_sdk.config.config_model и module_sdk.storage.storage (чтобы обойти глобальный config)

  4. Возвращает чистый модуль storage

Этот подход нужен из-за того, что config создается на уровне модуля при импорте.

Пример теста для вашего модуля

import importlib
import sys
import pytest


@pytest.fixture
def storage_module(tmp_path, monkeypatch):
"""Фикстура для чистого storage с тестовым конфигом."""
config_file = tmp_path / "config.yaml"
config_file.write_text("""
additional:
port: 8080
max_chunk_size: 10
api_url: "https://mock-api.test"
api_token: "test-token"
""".strip())

monkeypatch.setenv("CONFIG_FILE", str(config_file))

# Сбросить кэшированные модули
sys.modules.pop("module_sdk.config.config_model", None)
sys.modules.pop("module_sdk.storage.storage", None)

return importlib.import_module("module_sdk.storage.storage")


@pytest.fixture
def storage(storage_module):
return storage_module.ScanStatusStorage()


def test_host_added_to_storage(storage):
from module_sdk.models.collection_host import CollectionHost

host = CollectionHost(
name="test-host",
responsible="00000000-0000-0000-0000-000000000000",
)
storage.append_host_in_storage(host)

assert storage.total_exist_assets_count() == 1